/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

import blueprint4j.utils.*;

public class Keyboard extends JDialog implements ActionListener {
    public static final boolean GUI_VIRTUAL_KEYBOARD = Settings.getBoolean("gui.keyboard.virtual.enable",false);
    public static final int KEYBOARD_SIZE_X = Settings.getInt("gui.keyboard.virtual.size.x",800);
    public static final int KEYBOARD_SIZE_Y = Settings.getInt("gui.keyboard.virtual.size.y",320);
    private boolean isPassword = false;
    
    private String key[][] = new String[][]{
        {"`","1","2","3","4","5","6","7","8","9","0","-","=","<<"},
        {"Tab","q","w","e","r","t","y","u","i","o","p","[","]","\\"},
        {"Cap","a","s","d","f","g","h","j","k","l",";","'","Ent",},
        {"Shft","z","x","c","v","b","n","m",",",".","/","Shft","Ok"},
        {"083","082","073","072","    Space    ","@",".co.za"}
    };
    private String key_s[][] = new String[][]{
        {"~","!","@","#","$","%","^","&","*","(",")","_","+","<<"},
        {"Tab","Q","W","E","R","T","Y","U","I","O","P","{","}","|"},
        {"Cap","A","S","D","F","G","H","J","K","L",":","\"","Ent"},
        {"Shft","Z","X","C","V","B","N","M","<",">","?","Shft","Ok"},
        {"083","082","073","072","    Space    ","@",".co.za"}
    };
    private String key_c[][] = new String[][]{
        {"`","1","2","3","4","5","6","7","8","9","0","-","=","<<"},
        {"Tab","Q","W","E","R","T","Y","U","I","O","P","[","]","\\"},
        {"Cap","A","S","D","F","G","H","J","K","L",";","'","Ent"},
        {"Shft","Z","X","C","V","B","N","M",",",".","/","Shft","Ok"},
        {"083","082","073","072","    Space    ","@",".co.za"}
    };
    
    private Component owner = null;
    private JPanel center = new JPanel();
    
    private String current_key[][] = key;
    private VectorButton btn_key = new VectorButton();
    private JTextArea input = new JTextArea(5,50);
    private JPasswordField password = new JPasswordField("");
    private static final Font BIGFONT = new Font("Arial",Font.PLAIN,20);
    
    private boolean shift_on = false, caps_on = false,passwordField = false;
    
    public Keyboard(JFrame owner,String name,boolean passwordField) {
        super(owner,"Virtual Keyboard - " + name);
        this.owner = owner;
        this.passwordField = passwordField;
        init();
    }
    
    public Keyboard(JFrame owner,String name) {
        this(owner,name,false);
    }
    
    public Keyboard(JDialog owner,String name,boolean passwordField) {
        super(owner,"Virtual Keyboard - " + name);
        this.owner = owner;
        this.passwordField = passwordField;
        init();
    }
    
    public Keyboard(JDialog owner,String name) {
        this(owner,name,false);
    }
    
    public Keyboard() {
        init();
    }
    
    private void init() {
        setModal(true);
        getContentPane().setLayout(new BorderLayout());
        
        if (passwordField) {
            getContentPane().add(password,BorderLayout.NORTH);
        } else {
            getContentPane().add(input,BorderLayout.NORTH);
        }
        getContentPane().add(center,BorderLayout.CENTER);
        build(center,key);
        
        setResizable(true);
        pack();
        setSize(new Dimension(KEYBOARD_SIZE_X,KEYBOARD_SIZE_Y));
        setLocationRelativeTo(owner);
        input.setFont(BIGFONT);
        password.setFont(BIGFONT);
    }
    
    public void setText(String text) {
        if (text == null){
            input.setText("");
            password.setText("");
        } else {
            input.setText(text);
            password.setText(text);
        }
    }
    
    public String getText() {
        if (passwordField) {
            return new String(password.getPassword());
        } else {
            return input.getText();
        }
    }
    
    private void build(JPanel center,String key[][]) {
        center.setLayout(new GridLayout(key.length,1));
        for (int i = 0;i < key.length;i++) {
            JPanel panel = new JPanel();
            //panel.setLayout(new GridLayout(1,key[i].length));
            for (int e = 0;e < key[i].length;e++) {
                JButton btn = new JButton(key[i][e]);
                btn.setFont(BIGFONT);
                btn_key.add(btn);
                btn.addActionListener(this);
                panel.add(btn);
            }
            center.add(panel);
        }
    }
    
    private void rebuild(String key[][]) {
        int cnt = 0;
        for (int i = 0;i < key.length;i++) {
            for (int e = 0;e < key[i].length;e++) {
                btn_key.get(cnt++).setText(key[i][e]);
            }
        }
    }
    
    public void actionPerformed(ActionEvent event) {
        String s_key = ((JButton)event.getSource()).getText();
        if (s_key.equals("Shft")) {
            if (shift_on) {
                rebuild(key);
            } else {
                rebuild(key_s);
            }
            shift_on = !shift_on;
        } else if (s_key.equals("Cap")) {
            if (caps_on) {
                rebuild(key);
            } else {
                rebuild(key_c);
            }
            caps_on = !caps_on;
        } else if (s_key.equals("<<")) {
            if (passwordField)  {
                password.setText(new String(password.getPassword()).substring(0,password.getPassword().length-1));
            } else {
                input.setText(input.getText().substring(0,input.getText().length()-1));
            }
        } else if (s_key.equals("Ent")) {
            input.append("\n");
            if (passwordField) {
                JOptionPane.showMessageDialog(owner,"Not Allowd");
            }
        } else if (s_key.equals("    Space    ")) {
            input.append(" ");
            password.setText((new String(password.getPassword())+" "));
        } else if (s_key.equals("Tab")) {
            input.append("\t");
            if (passwordField) {
                JOptionPane.showMessageDialog(owner,"Not Allowd");
            }
        } else if (s_key.equals("Ok")) {
            dispose();
        } else {
            input.append(caps_on?(s_key.toUpperCase()):s_key);
            password.setText(new String(password.getPassword())+(caps_on?(s_key.toUpperCase()):s_key));
        }
    }
    
    public static class VectorButton {
        private Vector store = new Vector();
        
        public void add(JButton btn) {
            store.add(btn);
        }
        
        public JButton get(int index) {
            return (JButton)store.get(index);
        }
    }
    
    public static void main(String args[]) throws Exception {
        Keyboard keyboard = new Keyboard();
        keyboard.pack();
        keyboard.setVisible(true);
    }
    
}
